﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbGenerator.Model.units.column;
using System.Reflection;

namespace DbGenerator.Model.attributes.types
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class TColumnAttribute : Attribute, IEquatable<TColumnAttribute>
    {
        #region ctors
        static TColumnAttribute()
        {
            _NamedPropertyNames = new string[]
            {
                "AutoIncrement"
                ,"DataType"
                ,"AllowNulls"
                ,"MaxLength"
            };
        }
        public TColumnAttribute(string columnName)
        {
            _ColumnName = columnName;
            _AllowNulls = true;
            _AutoIncrement = false;
            _DataType = SqlDbType.Int;
            _MaxLength = Int32.MinValue;
        }
        #endregion

        #region private fields
        private static String[] _NamedPropertyNames;
        #endregion

        #region properties
        private string _ColumnName;
        public string ColumnName
        {
            get { return _ColumnName; }
        }

        private bool _AutoIncrement;
        public bool AutoIncrement
        {
            get { return _AutoIncrement; }
            set { _AutoIncrement = value; }
        }

        private SqlDbType _DataType;
        public SqlDbType DataType
        {
            get { return _DataType; }
            set { _DataType = value; }
        }

        private bool _AllowNulls;
        public bool AllowNulls
        {
            get { return _AllowNulls; }
            set { _AllowNulls = value; }
        }

        private int _MaxLength;
        public int MaxLength
        {
            get { return _MaxLength; }
            set { _MaxLength = value; }
        }
        #endregion

        #region static methods
        public static PropertyInfo[] GetNamedProperties()
        {
            List<PropertyInfo> r = new List<PropertyInfo>();
            int namedPropertyCount = _NamedPropertyNames.Length;
            for (int i = 0; i < namedPropertyCount; i++)
            {
                r.Add(typeof(TColumnAttribute).GetProperty(_NamedPropertyNames[i]));
            }
            if (r.Count > 0)
                return r.ToArray();
            return null;
        }
        #endregion

        #region IEquatable<TColumnAttribute> Members

        public bool Equals(TColumnAttribute other)
        {
            return _AllowNulls == other.AllowNulls
                && _AutoIncrement == other.AutoIncrement
                && _ColumnName == other.ColumnName
                && _DataType == other.DataType
                && _MaxLength == other.MaxLength;
        }

        #endregion
    }
}
